import textHtml from 'ngtemplate!html!../../../tpls/validation/text.html';
import phoneHtml from 'ngtemplate!html!../../../tpls/validation/phone.html';

class CompanyEditController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, CompanyService, getCompanyDetail, fileReader, iRunner){

    	var vm = this;

    	vm.$rootScope       = $rootScope;
        vm.$scope           = $scope;
    	vm.$cookieStore     = $cookieStore;
    	vm.$timeout 	    = $timeout;
    	vm.$state   	    = $state;
    	vm.ngDialog 	    = ngDialog;
    	vm.dialogService    = dialogService;
    	vm.CompanyService   = CompanyService;
        vm.getCompanyDetail = getCompanyDetail;
    	vm.fileReader       = fileReader;
    	vm.iRunner 	   	    = iRunner;

    	vm.textHtml  = textHtml;
    	vm.phoneHtml = phoneHtml;

        // alert($rootScope.is_admin);
        // 超级管理员
        vm.is_admin = $rootScope.is_admin;

    	// 机构类型
        vm.company_types = iRunner.raceScaleConfig;

        // 表单数据初始化
        vm.company = {
            company_name: '',
            company_city: '',
            company_num: '',
            radio_num: '',
            company_scale: '',
            radio_scale: '',
            company_type: '',
            company_phone: '',
            company_email: ''
        }

        // 监听模型变化, 显示每年赛事场戏
        $scope.$watch(angular.bind(vm, () => vm.company.radio_num ), (newValue, oldValue) => {
            if (newValue) {
                if (newValue==0) {
                    vm.company.company_num = iRunner.raceFrequencyConfig.number[0];
                }
                if (newValue==1) {
                    vm.company.company_num = iRunner.raceFrequencyConfig.number[1];
                }
                if (newValue==2) {
                    vm.company.company_num = iRunner.raceFrequencyConfig.number[2];
                }
                console.log(newValue)
            }
        });

        // 监听模型变化, 显示主要赛事规模
        $scope.$watch(angular.bind(vm, () => vm.company.radio_scale ), (newValue, oldValue) => {
            if (newValue) {
                if (newValue==0) {
                    vm.company.company_scale = iRunner.raceFrequencyConfig.scale[0];
                }
                if (newValue==1) {
                    vm.company.company_scale = iRunner.raceFrequencyConfig.scale[1];
                }
                if (newValue==2) {
                    vm.company.company_scale = iRunner.raceFrequencyConfig.scale[2];
                }
                if (newValue==3) {
                    vm.company.company_scale = iRunner.raceFrequencyConfig.scale[3];
                }
                console.log(newValue)
            }
        });

        // 获取当前赛事机构基本信息
        if (vm.getCompanyDetail.code==0) {

        	var Detail = vm.getCompanyDetail.data;

            vm.company.company_name = Detail.company_name;
            vm.company.company_city = Detail.city;
            vm.company.company_phone = Detail.user_cell;
            vm.company.company_email = Detail.user_mail;
            vm.imageSrc = Detail.logo;


            angular.forEach(vm.company_types, function(value, index) {
                if (Detail.type==value.id) {
                    vm.company.company_type = value.id;
                    console.log(value)
                }
            });

            angular.forEach(iRunner.raceFrequencyConfig.number, function(value, index) {
                if (Detail.race_frequency == value) {
                    vm.company.company_num = Detail.race_frequency;
                    vm.company.radio_num = index;
                }
            });

            angular.forEach(iRunner.raceFrequencyConfig.scale, function(value, index) {
                if (Detail.race_scale == value) {
                    vm.company.company_scale = Detail.race_scale;
                    vm.company.radio_scale = index;
                }
            });
        }

    }

    // 表单提交
    submitForm(isValid) {
        var vm = this;
        // 验证失败, 提示用户
        if (!isValid) {
            
            if (!vm.company.company_name) {
                $.blink(angular.element(document.getElementsByName('companyName')), 5, 100)
            } else if (!vm.company.company_type) {
                $.blink(angular.element(document.getElementsByName('companyType')), 5, 100)
            } else if (!vm.company.company_phone) {
                $.blink(angular.element(document.getElementsByName('companyPhone')), 5, 100)
            }            
        }

        // 验证通过
        if (isValid) {

            // 拼接POST数据格式
            var params = {
                company_name: vm.company.company_name,
                type: vm.company.company_type,
                logo: vm.myFile,
                city: vm.company.company_city,
                user_mail: vm.company.company_email,
                user_cell: vm.company.company_phone,
                race_scale: vm.company.company_scale,
                race_frequency: vm.company.company_num
            }

            console.log(params)

            // 更新公司
            vm.CompanyService.postCompanyUpdate(params, vm.$cookieStore.get('access_token'), vm.$state.params.company_id)
            .then( (data) => {

                // 成功调用
                data.code==0&&vm.dialogService.success('更新成功', 'dashboard.companylist');

                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
                
                console.log(data)
            }, (data) => {
                console.log('error')
            });

        }

        return false;
    }

    // 获取上传图片的地址url，用以实现预览图片
    getFile() {
        var vm = this;
        vm.fileReader.readAsDataUrl(vm.$scope.file, vm.$scope)
        .then( (result) => {
            console.log(vm.$scope.file)

            //判断附件是否为图片
            if(!/image\/\w+/.test(vm.$scope.file.type)){
                vm.dialogService.error('图片类型必须是.gif,jpeg,jpg,png中的一种哦');
                return false;
            } else {
                vm.imageSrc = result;
            }
            
        }, (data) => {
            console.log('error')
        });
    }
}

CompanyEditController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'CompanyService', 'getCompanyDetail', 'fileReader', 'iRunner']; 

export default CompanyEditController;